package com.example;



import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.*;
import com.rabbitmq.client.QueueingConsumer.Delivery;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Server {

    public static final String RPC_QUEUE_NAME = "rpc_queue";

    public static String sayHello(String name){
        return "hello " + name ;
    }

    public static void main(String[] args) throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException,TimeoutException {

        ConnectionFactory connFac = new ConnectionFactory() ;
        connFac.setHost("192.168.1.106");
        connFac.setPort(5672);
        connFac.setUsername("root");
        connFac.setPassword("root");
        Connection conn = connFac.newConnection() ;

        Channel channel = conn.createChannel() ;

        channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null) ;

        QueueingConsumer consumer = new QueueingConsumer(channel);

        channel.basicConsume(RPC_QUEUE_NAME, false , consumer) ;

        while(true){
            System.out.println("服务端等待接收消息..");
            Delivery deliver = consumer.nextDelivery() ;
            System.out.println("服务端成功收到消息..");
            BasicProperties props =  deliver.getProperties() ;

            String message = new String(deliver.getBody() , "UTF-8") ;
            System.out.println("接受的消息"+message);
            String responseMessage = sayHello(message) ;

            BasicProperties responseProps = new BasicProperties.Builder()
                    .correlationId(props.getCorrelationId())
                    .build() ;

            //将结果返回到客户端Queue
            channel.basicPublish("", props.getReplyTo() , responseProps , responseMessage.getBytes("UTF-8") ) ;

            //向客户端确认消息
            channel.basicAck(deliver.getEnvelope().getDeliveryTag(), false);
            System.out.println("服务端返回消息完成..");
        }

    }

}

